/**
 * 
 */
package jm.lib.lang;

/**
 * @author Jiming Liu
 *
 */
public class MutableInteger extends Number implements Comparable<MutableInteger>{

	/**
	 * 
	 */
	private static final long serialVersionUID = 2116253666723781029L;

	private int value;
	
	public MutableInteger(int value) {
		this.value = value;
	}
	
	public MutableInteger(String value) throws NumberFormatException {
		this.value = Integer.parseInt(value);
	}
	
	/* (non-Javadoc)
	 * @see java.lang.Number#intValue()
	 */
	@Override
	public int intValue() {
		return value;
	}

	/* (non-Javadoc)
	 * @see java.lang.Number#longValue()
	 */
	@Override
	public long longValue() {
		return (long)value;
	}

	/* (non-Javadoc)
	 * @see java.lang.Number#floatValue()
	 */
	@Override
	public float floatValue() {
		return (float)value;
	}

	/* (non-Javadoc)
	 * @see java.lang.Number#doubleValue()
	 */
	@Override
	public double doubleValue() {
		return (double)value;
	}

	public int getValue() {
		return value;
	}

	public void setValue(int value) {
		this.value = value;
	}

	public int increment() {
		return ++value;
	}
	
	public int add(int v) {
		value += v;
		return value;
	}

	public int decrement() {
		return --value;
	}

	public int compareTo(MutableInteger o) {
		return (this.value<o.value? -1 : (this.value==o.value ? 0 : 1));
	}
	
	public int multiply(int v) {
		value *= v;
		return value;
	}

	public int divide(int v) {
		value /= v;
		return value;
	}

	public int mod(int v) {
		value %= v;
		return value;
	}

	/**
	 * Override hashCode.
	 *
	 * @return the Objects hashcode.
	 */
	public int hashCode() {
		int hashCode = super.hashCode();
		hashCode = 31
			* hashCode
			+ (int) (+serialVersionUID ^ (serialVersionUID >>> 32));
		hashCode = 31 * hashCode + value;
		return hashCode;
	}

	/**
	 * Returns <code>true</code> if this <code>MutableInteger</code> is the same as the o argument.
	 *
	 * @return <code>true</code> if this <code>MutableInteger</code> is the same as the o argument.
	 */
	public boolean equals(Object o) {
		if (this == o) {
			return true;
		}
		if (!super.equals(o)) {
			return false;
		}
		if (o == null) {
			return false;
		}
		if (o.getClass() != getClass()) {
			return false;
		}
		MutableInteger castedObj = (MutableInteger) o;
		return ((this.value == castedObj.value));
	}



	public String toString() {
		return String.valueOf(this.value);
	}
}
